package com.xb.loan.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * @author lottery
 *
 */
public class DateHelper {

	/**
	 * 将日期格式化成字符串日期
	 * 
	 * @param format
	 * @param date
	 * @return
	 */
	public static String formatDate(String format, Date date) {
		if (StringUtils.isNull(format)) {
			format = DEFAULT_FORMAT;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	/**
	 * 指定格式的字符串日期转化为日期
	 * 
	 * @param format
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date convertToDate(String format, String date) throws ParseException {
		if (StringUtils.isNull(format)) {
			format = DEFAULT_FORMAT;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(date);
	}

	/**
	 * 将数字的日期转化为字符日期
	 * 
	 * @param format
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String convertToStrDate(String format, long date) {
		if (StringUtils.isNull(format)) {
			format = DEFAULT_FORMAT;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public final static String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static Date StringToDate(String dateString, String formatString) {
		if (StringUtils.isNull(formatString)) {
			formatString = DEFAULT_FORMAT;
		}
		SimpleDateFormat df = new SimpleDateFormat(formatString);
		try {
			Date date = df.parse(dateString);
			return date;
		} catch (Exception ex) {
			return null;
		}
	}

	public static String DateToString(String formatString, Date datetime) {

		if (StringUtils.isNull(formatString)) {
			formatString = DEFAULT_FORMAT;
		}
		SimpleDateFormat df = new SimpleDateFormat(formatString);
		return df.format(datetime);
	}

	/**
	 * 得到一天的开始时间
	 * 
	 * @param date
	 * @return
	 */
	public static String getTodayStartTime(Date date) {

		return DateToString("yyyy-MM-dd 00:00:00", date);
	}

	/**
	 * 得到一天的最后时间
	 * 
	 * @param date
	 * @return
	 */
	public static String getTodayEndTime(Date date) {
		return DateToString("yyyy-MM-dd 23:59:59", date);
	}

	/**
	 * 当前月的开始时间
	 * 
	 * @return
	 */
	public static String getCurrentMonthStart() {
		return DateToString("yyyy-MM-01 00:00:00", new Date());
	}

	/**
	 * 当前月的开始时间格式为yyyy-MM-01
	 * 
	 * @return
	 */
	public static String getCurrentMonthDay() {
		return DateToString("yyyy-MM-01", new Date());
	}

	/**
	 * 当前天的前一天
	 * 
	 * @return
	 */
	public static String getCurrentYesterday() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1); // 得到前一天
		Date date = calendar.getTime();
		return DateToString("yyyy-MM-dd", date);
	}

	/**
	 * 判断是否比当前时间晚
	 * 
	 * @param time
	 * @return
	 */
	public static boolean afterNow(String time, String format) {
		boolean result = false;
		if (StringUtils.isNull(format)) {
			format = DEFAULT_FORMAT;
		}
		Date date = DateHelper.StringToDate(time, format);
		if (date.getTime() - System.currentTimeMillis() > 0) {
			result = true;
		}

		return result;
	}

	/**
	 * 判断是否比当前时间早
	 * 
	 * @param time
	 * @return
	 */
	public static boolean beforeNow(String time, String format) {
		boolean result = false;
		Date date = DateHelper.StringToDate(time, format);
		if (date.getTime() - System.currentTimeMillis() < 0) {
			result = true;
		}

		return result;
	}

	/**
	 * 获取在指定日期的几月前或几月后
	 * 
	 * @param date
	 *            指定日期
	 * @param num
	 *            正：几个月后；负：几个月前
	 * @return
	 */
	public static Date getBeforeMonth(Date date, Integer num) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, num);
		return c.getTime();
	}

	/**
	 * 获取几天前或几天后
	 * 
	 * @param date
	 * @param num
	 * @return
	 */
	public static Date getBeforeDay(Date date, Integer num) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_MONTH, num);
		return c.getTime();
	}

	/**
	 * 获取几分钟前或几分钟后
	 * 
	 * @param date
	 * @param num
	 * @return
	 */
	public static Date getBeforeMinute(Date date, Integer num) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, num);
		return c.getTime();
	}

	/**
	 * 获取几小时前或几小时后
	 * 
	 * @param date
	 * @param num
	 * @return
	 */
	public static Date getBeforeHour(Date date, Integer num) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.HOUR, num);
		return c.getTime();
	}

	/**
	 * 
	 * 获取某个日期所在月份的第一天
	 * 
	 * @param date
	 * @return 2015年10月30日 by ziqiang.zhang
	 */
	public static Date getFistDayOfMonth(Date date) {

		Calendar cale = Calendar.getInstance();
		cale.setTime(date);
		cale.set(Calendar.DAY_OF_MONTH, 1);

		return cale.getTime();
	}

	/**
	 * 
	 * 获取某个日期所在月最后一天
	 * 
	 * @param date
	 * @return 2015年10月30日 by ziqiang.zhang
	 */
	public static Date getLastDayOfMonth(Date date) {
		Calendar cale = Calendar.getInstance();
		cale.setTime(date);
		cale.set(Calendar.DAY_OF_MONTH, cale.getActualMaximum(Calendar.DAY_OF_MONTH));

		return cale.getTime();
	}

	/**
	 * 获取当前String格式时间的第一天
	 * 
	 * @param endTime
	 *            yyyy-MM-dd
	 * @return 当前月份的第一天 2016年1月5日 by wuqinglong
	 */
	public static String getCurrentMonthDayByEndTime(String endTime) {
		if (endTime != null && endTime.length() > 0) {
			return endTime.substring(0, endTime.length() - 2) + "01";
		} else {
			return getCurrentMonthDay();
		}
	}
	/**
	 * 
	 * 返回当前固定时间
	 * @param hour
	 * @param min
	 * @param sec
	 * @return
	 * 2016年8月1日 by ziqiang.zhang
	 */
	public static Date getFixOfToday(int hour ,int min , int sec){
		
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, hour);
		c.set(Calendar.MINUTE, min);
		c.set(Calendar.SECOND, sec);
		
		return c.getTime();
		
	}

	public static Long getTimeStampByTime(String dateStr){
		if(dateStr != null && dateStr.length() > 0){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = null;
			try {
				date = format.parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}

			if(date != null){
				return date.getTime();
			}
		}
		return -1L;
	}

	/*
     * 将时间戳转换为时间
     */
	public static String stampToDate(Long dateLong){
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date(dateLong);
		res = simpleDateFormat.format(date);
		return res;
	}
	
	public static void main(String[] args) {

		/*
		 * @SuppressWarnings("unused") Date date=new Date(1271159100000l);
		 * System.out.println(DateHelper.DateToString("yyyyMMdd",
		 * DateHelper.getBeforeDay(new Date(), -1)));
		 */

		System.out.println(getFixOfToday(3, 0, 0));

	}

}
